package com.health.ts.controller.flup;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.ts.constant.CustomPage;
import com.health.ts.constant.FrontPage;
import com.health.ts.constant.ApiResult;
import com.health.ts.controller.BaseController;
import com.health.ts.entity.consultation.PatientInfo;
import com.health.ts.entity.flup.Flup;
import com.health.ts.entity.flup.VFlupList;
import com.health.ts.service.consultation.PatientInfoService;
import com.health.ts.service.flup.FlupService;
import com.health.ts.service.flup.VFlupListService;
import com.health.ts.utils.AssertUtil;
import com.health.ts.utils.GenerateIdUtil;
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2017-09-06
 */
@RestController
@RequestMapping(value = "/api/flup",produces= BaseController.JSON_UTF8)
public class FlupController extends BaseController{
	
	@Autowired
	FlupService flupService;
	
	@Autowired
	VFlupListService  vflupListService;
	
	@Autowired
	PatientInfoService  patientInfoService;
	
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public ApiResult addFlup(@RequestBody Flup flup) {
		flup.setId(GenerateIdUtil.makeID());
		flup.setFlup_time(new Date());
		String pid=flup.getPid();
		if(patientInfoService.getById(pid)!=null){
			PatientInfo patientInfo=patientInfoService.getById(pid);
			patientInfo.setIsFlup("2");
			patientInfoService.updateById(patientInfo);
		}
		//更新患者表
		flupService.save(flup);
		return ApiResult.success(flup);
	}
	
	@RequestMapping(value="/select/{Id}", method=RequestMethod.GET)
	public ApiResult selectFlup(@PathVariable String Id) {
		VFlupList vflupList=vflupListService.getById(Id);
		AssertUtil.notNull(vflupList, "要查询的随访不存在！");
		return ApiResult.success(vflupList);
	}
	
	/**
	 * 查询首次待随访列表
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listFirst", method=RequestMethod.POST)
	public ApiResult listFlupPager(FrontPage page) {
		QueryWrapper<VFlupList> wrapper = new QueryWrapper<VFlupList>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.eq("idnumber", idnumber); 
		}
		wrapper.eq("isFlup", "1");//未随访
		Page<VFlupList> pageList = vflupListService.page(page.getPagePlus(), wrapper);
		CustomPage<VFlupList> customPage = new CustomPage<VFlupList>(pageList);
		return ApiResult.success(customPage);
	}
	
	/**
	 * 随访结束
	 * @param page
	 * @return
	 */
	@RequestMapping(value="/listFinsh", method=RequestMethod.POST)
	public ApiResult listFlupFinshPager(FrontPage page) {
		QueryWrapper<VFlupList> wrapper = new QueryWrapper<VFlupList>();
		String patient_name =getParam(page,"patient_name");
		if (StringUtils.isNotEmpty(patient_name)){
			wrapper.like("patient_name", patient_name); 
		}
		String idnumber =getParam(page,"idnumber");
		if (StringUtils.isNotEmpty(idnumber)){
			wrapper.eq("idnumber", idnumber); 
		}
		wrapper.eq("isFlup", "2");//已随访
		Page<VFlupList> pageList = vflupListService.page(page.getPagePlus(), wrapper);
		CustomPage<VFlupList> customPage = new CustomPage<VFlupList>(pageList);
		return ApiResult.success(customPage);
	}
}
